import { z } from 'zod'

// Layer 样式定义
export const layerStyleSchema = z.object({
  fontSize: z.string().optional(),
  fontFamily: z.string().optional(),
  fontWeight: z.string().optional(),
  fontStyle: z.string().optional(),
  lineHeight: z.string().optional(),
  letterSpacing: z.string().optional(),
  textAlign: z.string().optional(),
  textDecoration: z.string().optional(),
  color: z.string().optional(),
  backgroundColor: z.string().optional(),
  borderColor: z.string().optional(),
  borderWidth: z.string().optional(),
  borderRadius: z.string().optional(),
  boxShadow: z.string().optional(),
  textShadow: z.string().optional(),
  opacity: z.string().optional(),
  padding: z.string().optional(),
  margin: z.string().optional(),
  rotation: z.string().optional(),
  filter: z.string().optional(),
  objectFit: z.string().optional(),
})

// Layer 元数据定义
export const layerMetadataSchema = z.object({
  confidence: z.number(),
  language: z.string().optional(),
  estimatedFont: z.string().optional(),
  imageType: z.string().optional(),
})

// Layer 位置定义
export const layerPositionSchema = z.object({
  x: z.number(),
  y: z.number(),
  width: z.number(),
  height: z.number(),
})

// 基础 Layer 定义（与 API layerSchema 保持一致）
export const baseLayerSchema = z.object({
  id: z.string(),
  type: z.enum(['text', 'image', 'shape']),
  content: z.string(),
  position: layerPositionSchema,
  style: layerStyleSchema.optional(),
  metadata: layerMetadataSchema.optional(),
  imageData: z.string().optional(), // Base64 图片数据
})

// TypeScript 类型导出
export type LayerStyle = z.infer<typeof layerStyleSchema>
export type LayerMetadata = z.infer<typeof layerMetadataSchema>
export type LayerPosition = z.infer<typeof layerPositionSchema>
export type BaseLayer = z.infer<typeof baseLayerSchema>

// 扩展的 Layer 定义（用于前端编辑器，包含扁平化的属性）
export interface Layer {
  id: string
  type: 'text' | 'image' | 'shape'
  content: string
  position: LayerPosition
  name?: string
  visible: boolean
  locked: boolean
  parentId?: string
  expanded?: boolean
  style?: LayerStyle
  metadata?: LayerMetadata
  imageData?: string

  // 扁平化的位置属性（方便访问）
  x: number
  y: number
  width: number
  height: number

  // 文本专用属性
  fontSize?: number
  fontFamily?: string
  fontWeight?: string
  textAlign?: string
  color?: string

  // 形状专用属性
  shapeType?: 'rectangle' | 'circle' | 'ellipse' | 'line'
  fill?: string
  stroke?: string
  strokeWidth?: number
  cornerRadius?: number

  // 图像专用属性
  opacity?: number
  description?: string
}

// 用于兼容新旧组件的类型定义
export interface TextLayer extends Layer {
  type: 'text'
  content: string
  fontSize?: number
  fontFamily?: string
  fontWeight?: string
  textAlign?: string
  color?: string
  x: number
  y: number
  width: number
  height: number
}

export interface ShapeLayer extends Layer {
  type: 'shape'
  content: string
  shapeType: 'rectangle' | 'circle' | 'ellipse' | 'line'
  fill?: string
  stroke?: string
  strokeWidth?: number
  cornerRadius?: number
  x: number
  y: number
  width: number
  height: number
}

export interface ImageLayer extends Layer {
  type: 'image'
  content: string
  imageData?: string
  description?: string
  opacity?: number
  x: number
  y: number
  width: number
  height: number
}

// 分析结果类型
export interface AnalysisResult {
  imageWidth: number
  imageHeight: number
  layers: Layer[]
  originalImage?: string
}
